package fr.tm.ima.client.gxt.grid;

/**
 * @author aquinault
 */

import org.puremvc.java.multicore.interfaces.IMediator;
import org.puremvc.java.multicore.patterns.facade.Facade;
import org.puremvc.java.multicore.patterns.observer.Notification;

import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.Widget;

import fr.tm.ima.client.gxt.grid.controller.FillModelCommand;
import fr.tm.ima.client.gxt.grid.controller.PrepViewCommand;
import fr.tm.ima.client.gxt.grid.view.GridMediator;

/**
 * Grid facade. 
 * <i>Singleton</i>
 */
public class GridFacade extends Facade {
	
	/**
	 * Start. <br>
	 * Body : Null
	 */
	public static final String STARTUP_COMMAND = "startup"; 
	
	/**
	 * Key of this facade.
	 */
	public static final String NAME = "GridFacade";
	
	/**
	 * Unique instance.
	 */
	private static GridFacade instance = null;

	/**
	 * Model changed command.
	 */
	public static final String MODEL_CHANGED = "ModelChanged"; 

	/**
	 * Fill model command.
	 */
	public static final String FILL_MODEL_COMMAND = "fillModelCommand"; 

	/**
	 * Constructor.
	 */
	protected GridFacade() {
		super(NAME);
	}
	
	/**
	 * get the instance.
	 * @return the singleton
	 */
	public static GridFacade getInstance() {
		if (instance == null) {
			instance = new GridFacade();
			instance.notifyObservers(new Notification(STARTUP_COMMAND));
		}
		return instance;
	}

	/**
	 * Show the facade
	 * @param panel Add the facade to the panel
	 */
	public void showIn(final Panel panel) {
		IMediator mediator = retrieveMediator(GridMediator.NAME);
		panel.add((Widget) mediator.getViewComponent());

	}
	
	/**
	 * Initialize controller.
	 * Register the commands.
	 */
	protected void initializeController() {
		super.initializeController();
		registerCommand(STARTUP_COMMAND, new PrepViewCommand());
		registerCommand(FILL_MODEL_COMMAND, new FillModelCommand());
	}

}
